package com.jdz.gxq.info.admin.role.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

import com.jdz.gxq.info.admin.role.bo.Menu;
import com.jdz.gxq.info.admin.role.bo.Role;
import com.jdz.gxq.info.admin.role.bo.RoleMenu;

public class RoleServiceImpl implements RoleService {

	private QueryDAO queryDAO = null;

	private UpdateDAO updateDAO = null;

	/**
	 * Role检索分页用，查询记录条数
	 * 
	 * @return
	 */
	public int getRolesCount(String name) {

		int totalCount = queryDAO.executeForObject("role.getRolesCount", name,
				Integer.class);

		return totalCount;
	}

	/**
	 * Role检索
	 * 
	 * @return
	 */
	public List<Role> getRoles(Map<String, Object> params) {

		return queryDAO.executeForObjectList("role.getRoles", params);
	}

	/**
	 * 追加Role
	 * 
	 * @param role
	 */
	public void insertRole(Role role) {

		updateDAO.execute("role.insertRole", role);
	}

	/**
	 * 更新Role
	 * 
	 * @param role
	 */
	public void updateRole(Role role) {

		updateDAO.execute("role.updateRole", role);
	}

	/**
	 * 通过id得到role
	 * 
	 * @param id
	 * @return
	 */
	public Role getRoleById(int id) {

		Role role = queryDAO.executeForObject("role.getRoleById", id,
				Role.class);

		return role;
	}

	/**
	 * 删除Role
	 * 
	 * @param role
	 */
	public void deleteRole(int id) {

		updateDAO.execute("role.deleteRoleById", id);
	}

	/**
	 * 验证roleName是否重复 flag: 1 追加时验证 flag: 2 更新时验证
	 */
	public boolean checkRoleName(Role paramRole, int flag) {

		Role role = queryDAO.executeForObject("role.getRoleByName",
				paramRole.getName(), Role.class);

		if (role == null) {
			return true;
		}

		// 更新验证，查出来的数据是不是本身
		if (flag == 2) {

			if (role.getId() == paramRole.getId()) {
				return true;
			}
		}

		return false;
	}
	
	/**
	 * 设置权限(读取权限菜单信息)
	 * @param roleId
	 * @param parentMenuId
	 * @return
	 */
	public List<Menu> getRoleMenuList(int roleId) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("roleId", roleId);
		params.put("parentMenuId", 0);
		
		List<Menu> menuList = queryDAO.executeForObjectList("role.getMenuListByRole", params);
		
		for (Menu menu : menuList) {
			params.put("parentMenuId", menu.getId());
			List<Menu> subMenuList = queryDAO.executeForObjectList("role.getMenuListByRole", params);
			menu.setMenuList(subMenuList);
		}
		
		return menuList;
	}
	
	/**
	 * 设置权限
	 * @param roleId
	 * @param roleMenuList
	 * @return
	 */
	public void setRoleMenus(int roleId, List<RoleMenu> roleMenuList, String modifiedBy) {
		
		// 先删除原来的权限设置
		updateDAO.execute("role.deleteRoleMenuByRole", roleId);
		
		// 插入新的权限设置
		for(RoleMenu roleMenu : roleMenuList) {
			if (roleMenu.getHasRole() == 1) {
				roleMenu.setRoleId(roleId);
				roleMenu.setModifiedBy(modifiedBy);
				updateDAO.execute("role.insertMenuRole", roleMenu);
			}
		}
	}
 
	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}

}
